import 'package:flutter/material.dart';
import 'package:flutter_picker/flutter_picker.dart';

import '../common/styles.dart';
import '../models/selectItem.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class CustomSelectWidget extends StatefulWidget {

  final List<SelectItems> items;
  final String title;
  final String icon;
  final Function selectCallback;
  final String rightText;
  final bool bottomLine;
  final bool require;
  final Color titleColor;
  final bool enable;
  final double titleFont;
  final bool rightArrow;
  final Color rightColor;
  CustomSelectWidget(
      {
        this.items,
        @required this.title,
        this.icon = '',
        this.enable = true,
        this.rightArrow = true,
        this.titleFont,
        this.titleColor,
        this.rightColor = Styles.DetailColor,
        this.selectCallback,
        this.rightText = '',
        this.bottomLine = false,this.require = false});

  @override
  _CustomSelectWidgetState createState() => _CustomSelectWidgetState();
}

class _CustomSelectWidgetState extends State<CustomSelectWidget> {

  String current = '';
  int currentIndex = 0;

  @override
  void initState() {
    super.initState();
    this.setState(() {
      current = widget.rightText;
    });
  }

  @override
  void didUpdateWidget(CustomSelectWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    this.setState(() {
      current = widget.rightText;
    });
  }
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
  }

  showSelectPicker(BuildContext context) {
    FocusScope.of(context).requestFocus(FocusNode());
    Picker(
      adapter: PickerDataAdapter(data: getPickerItems()),
      hideHeader: false,
      textAlign: TextAlign.left,
      backgroundColor: Colors.white,
      selecteds: [currentIndex],
      onConfirm: (Picker picker, List value) {
        if(widget.selectCallback!=null){
          widget.selectCallback(widget.items[value[0]]);
          this.setState(() {
            current = widget.items[value[0]].label;
            currentIndex = value[0];
          });
        }
      },
      confirmText: "确定",
      cancelText: "取消",
      confirmTextStyle:Theme.of(context).textTheme.body1,
      cancelTextStyle: Theme.of(context).textTheme.body1,

    ).showModal(context);

  }
  List<PickerItem> getPickerItems() {
    List<PickerItem> list = List();

    for (SelectItems item in widget.items) {
      list.add(PickerItem(text: Text(item.label,overflow: TextOverflow.ellipsis,style: Theme.of(context).textTheme.title,), value: item.value));
    }

    return list;
  }
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Container(
        padding:
        EdgeInsets.fromLTRB(Styles.spacing8, 2, 0, 2),
        color: Theme.of(context).cardColor,
        height: Styles.MainSelectRowHeight,
        child: DecoratedBox(
          decoration: BoxDecoration(
              border: widget.bottomLine
                  ? Border(
                  bottom: BorderSide(color: Styles.LineColor, width: 1))
                  : null),
          child: Padding(
            padding: EdgeInsets.fromLTRB(Styles.spacing8, 0, Styles.spacing16, 0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    widget.require
                        ? Text(
                      '*',
                      style: TextStyle(color: Colors.red),
                    )
                        : Container(),
                    widget.icon != ''
                        ? Image(
                      image: AssetImage(widget.icon),
                      width: 20,
                      height: 20,
                      fit: BoxFit.contain,
                    )
                        : Container(),
                    Padding(
                      padding: EdgeInsets.all(0),
                      child: Text(
                        widget.title,
                        style: TextStyle(
                            color: widget.titleColor!=null?widget.titleColor:Styles.DetailColor,
                            fontSize: widget.titleFont!=null?widget.titleFont:Styles.MiddleFont17),
                      ),
                    ),
                  ],
                ),
                Expanded(child: Row(
                  children: <Widget>[
                    Expanded(child: Text(
                      (current == ''||current == null) ? '请选择' : current,
                      textAlign: TextAlign.end,
                      style: TextStyle(
                          fontSize: widget.titleFont!=null?widget.titleFont:Styles.MiddleFont17,
                          color:widget.enable?widget.rightColor:Styles.LeftTitleColor),
                      overflow: TextOverflow.ellipsis,
                    )),
                    widget.rightArrow?Image(
                      image: AssetImage('assets/images/right_arrow.png'),
                      width: 20.w,
                      height: 20.h,
                      fit: BoxFit.contain,
                      color: widget.enable?widget.rightColor:Styles.LeftTitleColor,
                    ):Container()
                  ],
                ))
              ],
            ),
          ),
        ),
      ),
      onTap:widget.enable? () => this.showSelectPicker(context):null,
    );
  }
}
